package com.bj58.groupbuy.action.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;

import com.bj58.groupbuy.action.charts.ChartService;
import com.bj58.groupbuy.action.charts.ViewResult;
import com.bj58.groupbuy.action.model.BelongLocal;
import com.bj58.groupbuy.action.model.ChinahrIndustry;
import com.bj58.groupbuy.action.model.ChinahrJob;
import com.bj58.groupbuy.action.model.ChinahrSource;
import com.bj58.groupbuy.action.model.DisplayLocal;
import com.bj58.groupbuy.action.model.IRColumns;
import com.bj58.groupbuy.action.model.IRReport;
import com.bj58.groupbuy.action.model.IRSearchConfig;
import com.bj58.groupbuy.action.model.InfoSourcePlatform;
import com.bj58.groupbuy.action.model.JxedtFlowSource;
import com.bj58.groupbuy.action.model.NetFlowSource;
import com.bj58.groupbuy.action.model.NewBelongCate;
import com.bj58.groupbuy.action.model.NewDisplayCate;
import com.bj58.groupbuy.action.model.OperMenu;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.service.IRColumnsService;
import com.bj58.groupbuy.action.service.IRReportService;
import com.bj58.groupbuy.action.service.IRSearchConfigService;
import com.bj58.groupbuy.action.service.IntReportMainService;
import com.bj58.groupbuy.action.service.UserOperService;
import com.bj58.groupbuy.action.util.MyPathUtil;
import com.bj58.groupbuy.action.util.MyStore;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.groupbuy.action.util.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.actionresult.ContentResult;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/intcharts")
public class IntChartsController extends MvcController{
	private  ChartService chartService = new ChartService();
	private IRReportService iRReportService = ServiceLocator.get(IRReportService.class);
	private IRColumnsService iRColumnsService = ServiceLocator.get(IRColumnsService.class);
	private IRSearchConfigService iRSearchConfigService = ServiceLocator.get(IRSearchConfigService.class);
	private IntReportMainService IntReportMainService = ServiceLocator.get(IntReportMainService.class);
	private UserOperService userOperService = ServiceLocator.get(UserOperService.class);

	private SomePublicUtil spu = new SomePublicUtil();
	
	static String templateMainPath = MyPathUtil.getIChartsPath()+"templateMain.html"; //模板主体
	static String templateSelectOnePath = MyPathUtil.getIChartsPath()+"templateSelectOne.html";//查询模板 类型1
	static String templateTableOnePath = MyPathUtil.getIChartsPath()+"templateTableOne.html";//展示模板 类型1
	static String cateSelectPath = MyPathUtil.getIChartsPath()+"cateSelect.html";//品类级联组件模板
	static String citySelectPath = MyPathUtil.getIChartsPath()+"citySelect.html";//城市级联组件模板
	static String dataPermissionsPath = MyPathUtil.getIChartsPath()+"dataPermissions.html";//数据资源权限模板
	static String infoSourcePath = MyPathUtil.getIChartsPath()+"sourceSelects.html";//信息/流量来源级联模板
	static String chrSelectPath = MyPathUtil.getIChartsPath()+"chinaHRSelects.html";//中华英才组件模块

	
	/** 报表模板生成    模板已存在则直接render  不存在则解析生产新模板
	 * @return
	 */
	@Path("/createForm")
	@POST
	public ActionResult createForm(){
		String content = "error";
		try {
			content = createSelectOne();
		} catch (Exception e) {
			e.printStackTrace();
			return new ContentResult("error");
		}
		return ViewResult.textView(content);
	}
	
	private String createSelectOne()throws Exception{
		HttpServletRequest request = beat.getRequest();
		String pathId = ServletRequestUtils.getStringParameter(request, "pathId","");
		String groupBy = ServletRequestUtils.getStringParameter(request, "groupBy","");
		int menuId = Integer.parseInt(pathId.split("_")[0]);
		IRReport  irReport = iRReportService.getReportListByMenuId(menuId);
		List<IRSearchConfig>  searchConfigList =   iRSearchConfigService.getListByIRid(irReport.getId());
			
		@SuppressWarnings("unchecked")
		Map<String,String[]> requestParamMap = request.getParameterMap();
		
		Map<String,Object> conMap = new HashMap<String, Object>();
		for(String key : requestParamMap.keySet()){
			conMap.put(key, requestParamMap.get(key)[0]);
		}
		conMap.put("pathId", pathId);//编号  构成form提交路径
		
		int listSize = 1; //查询模块高度指标  默认为1   typeId为5、6、7则4个为一行  增加高度
		
		List<IRSearchConfig> searchList = new ArrayList<IRSearchConfig>();  //单字段模糊查询、单字段精准查询、单字段下拉框 三种查询的集合
		
		String cateSelectValue  = "";//品类级联模板内容
		String citySelectValue  = "";//城市级联模板内容
		String infoSourceSelectValue  = "";//信息来源级联模板内容
		String flowSourceSelectValue  = "";//流量来源级联模板内容
		String jxedtFlowSourceSelectValue  = "";//驾校一点通流量来源级联模板内容
		String chrFlowSourceSelectValue  = "";//中华英才流量来源级联模板内容
		String chrIndustrySelectValue  = "";//中华英才行业级联模板内容
		String chrJobSelectValue  = "";//中华英才职位级联模板内容
		HashMap<String,String>  gbMap = new HashMap<String, String>(); //分组维度的中英文对照		
		for(IRSearchConfig vo : searchConfigList ){			
			int typeId = vo.getSearchTypeId();
			switch (typeId) {
			case 7: //单字段下拉
				if(groupBy.contains(vo.getSename())){
					if(vo.getDetailValue().contains("|")){
						String tempDV = vo.getDetailValue();
						vo.setDetailValue(tempDV.substring(0,tempDV.indexOf("|")));
					}
					searchList.add(vo);
				}
				break;
			case 8://分组维度	
				String gbDic = vo.getDetailValue();//groupby的中英文对照
				for(String dic : gbDic.split(";")){
					String ename = dic.split(":")[0];
					String cname = dic.split(":")[1];
					gbMap.put(ename, cname);
				}
				break;
			case 11: //展现城市级联
			case 12://归属城市级联
				conMap.put("citySelect", 1);
				citySelectValue = dealCitySelect(request,vo,groupBy);
				break;
			case 13://图表
				conMap.put("hasChart", 1);
				listSize ++;
				break;
			case 17: //展现品类级联 - new
			case 18://归属品类级联 - new 
				conMap.put("cateSelect", 1);
				cateSelectValue = dealCateSelect(request,vo,groupBy);
				break;
			case 19://信息来源组件
				conMap.put("infoSourceSelect", 1);
				infoSourceSelectValue = dealInfoSourceSelect(request, vo, groupBy);
				listSize ++;
				break;
			case 20://流量来源组件
				conMap.put("flowSourceSelect", 1);
				flowSourceSelectValue = dealFlowSourceSelect(request, vo,groupBy);
				listSize ++;
				break;
			case 21://驾校一点通流量来源组件
				conMap.put("jxedtFlowSourceSelect", 1);
				jxedtFlowSourceSelectValue = dealJxedtSourceSelect(request, vo,groupBy);
				listSize ++;
				break;
			case 22://中华英才流量来源组件
				conMap.put("chrFlowSourceSelectValue", 1);
				chrFlowSourceSelectValue = dealCHRFlowSourceSelect(request, vo, groupBy);
				listSize ++;
				break;
			case 23://中华英才行业组件
				conMap.put("chrIndustrySelectValue", 1);
				chrIndustrySelectValue = dealCHRIndustrySelect(request, vo, groupBy);
				listSize ++;
				break;
			case 24://中华英才职位组件
				conMap.put("chrJobSelectValue", 1);
				chrJobSelectValue = dealCHRJobSelect(request, vo, groupBy);
				listSize ++;
				break;
			}
			
		}
		
		List<String> cnameList =  new ArrayList<String>();//中文名
		List<IRColumns>  irColList = iRColumnsService.getColsByReportId(irReport.getId());
		if(irReport.getTemplateType() == 1){  //默认显示全部字段
			for(IRColumns column : irColList){
				cnameList.add(column.getCname()+":"+column.getColType()+":"+column.getEname()); //字段中文名,字段类型,英文名
			}
		}else if(irReport.getTemplateType() == 2){  //根据选择 查询、显示字段
			String colsSelect  =  ServletRequestUtils.getStringParameter(request, "colsSelect","");//查询项勾选值
			HashSet<String> colsSelectSet = new HashSet<String>();
			String[] colsTempArray = colsSelect.split(",");
			for (String col : colsTempArray) {
				colsSelectSet.add(col);
			}
				
			for(IRColumns column : irColList){
				if(colsSelectSet.contains(column.getEname()) || "groupBy".equals(column.getEname())){
					cnameList.add(column.getCname()+":"+column.getColType()+":"+column.getEname()); //字段中文名及字段类型
				}
			}
		}
		//groupby不为空时 && 表头集合第一个为分组维度时
		if(!"".equals(groupBy) && cnameList.get(0).contains("维度") ){
			//多groupby处理
			List<String> newCnameList =  new ArrayList<String>();
			for(int i = 0 ; i < groupBy.split(",").length; i ++){
				String gb = groupBy.split(",")[i];
				newCnameList.add(gbMap.get(gb)+":0");
			}
			cnameList.remove(0);
			newCnameList.addAll(cnameList);
			cnameList = newCnameList;
		}
		conMap.put("cnameList",cnameList);
		
		listSize = listSize+(searchList.size() /4);
		if(searchList.size() % 4 > 0 ){
			listSize++;
		}
		conMap.put("searchList", searchList);
		conMap.put("cateSelectValue", cateSelectValue);
		conMap.put("citySelectValue", citySelectValue);
		conMap.put("infoSourceSelectValue", infoSourceSelectValue);
		conMap.put("flowSourceSelectValue", flowSourceSelectValue);
		conMap.put("jxedtFlowSourceSelectValue", jxedtFlowSourceSelectValue);
		conMap.put("chrFlowSourceSelectValue", chrFlowSourceSelectValue);
		conMap.put("chrIndustrySelectValue", chrIndustrySelectValue);
		conMap.put("chrJobSelectValue", chrJobSelectValue);
		
		String main = MyPathUtil.getTemByPath(templateSelectOnePath);
		String content  = VelocityTools.render(main, conMap);
		content = content.replace("#!", "$!");
		content = content.replace("&^", "#");
		return content;
	}
	
	/**
	 * 驾校一点通级联组件内容获取
	 */
	private String dealJxedtSourceSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";

		selectMap.put("showJxedtSourceSelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showJxedtSource"+(i+1), 1);
			}
		}
		int jxedtShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("jxedtShowType", jxedtShowType);

		String jxedtFlowSourcePlatform = "";//平台
		for(String temp : vo.getDetailValue().split(";")){
			if(temp.split(":")[0].equals("platform")){
				jxedtFlowSourcePlatform = temp.split(":")[1];
			}
		}
		selectMap.put("jxedtFlowSourcePlatform", jxedtFlowSourcePlatform);
		//source1
		String jxedtSourceOne = ServletRequestUtils.getStringParameter(request,"jxedtSourceOne" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showJxedtSource1")){	
			List<JxedtFlowSource> jxedtSourceOneList = chartService.getJxedtSource1List(jxedtFlowSourcePlatform,jxedtSourceOne);
			jxedtSourceOne = jxedtSourceOneList.get(0).getSource1();
			selectMap.put("jxedtSourceOneList",jxedtSourceOneList);
		}else{
			selectMap.put("jxedtSourceOne",jxedtSourceOne);
		}
		
		//source2
		String jxedtSourceTwo = ServletRequestUtils.getStringParameter(request,"jxedtSourceTwo" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showJxedtSource2")){	
			List<JxedtFlowSource> jxedtSourceTwoList = chartService.getJxedtSource2List(jxedtFlowSourcePlatform,jxedtSourceOne,jxedtSourceTwo);
			jxedtSourceTwo = jxedtSourceTwoList.get(0).getSource2();
			selectMap.put("jxedtSourceTwoList",jxedtSourceTwoList);
		}else{
			selectMap.put("jxedtSourceTwo",jxedtSourceTwo);
		}
		
		path = infoSourcePath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	/**
	 *中华英才行业级联组件内容获取
	 */
	private String dealCHRIndustrySelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";
		
		selectMap.put("showCHRIndustrySelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showCHRIndustry"+(i+1), 1);
			}
		}
		int industryShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("industryShowType", industryShowType);
		
		//一级来源  
		String chrIndustryOne = ServletRequestUtils.getStringParameter(request,"chrIndustryOne" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRIndustry1")){ 
			List<ChinahrIndustry> chrIndustryOneList = chartService.getCHRIndustryCate1(chrIndustryOne);
			chrIndustryOne = chrIndustryOneList.get(0).getCate1_name();
			selectMap.put("chrIndustryOneList",chrIndustryOneList);
		}else{
            selectMap.put("chrIndustryOne",chrIndustryOne);
        }
		
		//二级来源
		String chrIndustryTwo = ServletRequestUtils.getStringParameter(request,"chrIndustryTwo" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRIndustry2")){		
			List<ChinahrIndustry> chrIndustryTwoList = chartService.getgetCHRIndustryCate2(chrIndustryOne,chrIndustryTwo);
			selectMap.put("chrIndustryTwoList",chrIndustryTwoList);
		}else{
            selectMap.put("chrIndustryTwo",chrIndustryTwo);
        }
		
		path = chrSelectPath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	/**
	 *中华英才职位级联组件
	 */
	private String dealCHRJobSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";
		
		selectMap.put("showCHRJobSelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showCHRJob"+(i+1), 1);
			}
		}
		int chrJobShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("chrJobShowType", chrJobShowType);
		
		//一级来源  
		String chrJobOne = ServletRequestUtils.getStringParameter(request,"chrJobOne" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRJob1")){		
			List<ChinahrJob> chrJobOneList = chartService.getCHRJobCate1(chrJobOne);
			chrJobOne = chrJobOneList.get(0).getCate1_name();
			selectMap.put("chrJobOneList",chrJobOneList);
		}else{
            selectMap.put("chrJobOne",chrJobOne);
        }
		
		//二级来源
		String chrJobTwo = ServletRequestUtils.getStringParameter(request,"chrJobTwo" ,"");
		if(selectMap.containsKey("showCHRJob2")){		
			List<ChinahrJob> chrJobTwoList = chartService.getCHRJobCate2(chrJobOne, chrJobTwo);
			chrJobTwo = chrJobTwoList.get(0).getCate2_name();
			selectMap.put("chrJobTwoList",chrJobTwoList);
		}else{
            selectMap.put("chrJobTwo",chrJobTwo);
        }	
		
		//三级来源
		String chrJobThree = ServletRequestUtils.getStringParameter(request,"chrJobThree" ,"");
		if(selectMap.containsKey("showCHRJob3")){		
			List<ChinahrJob> chrJobThreeList = chartService.getCHRJobCate3(chrJobOne, chrJobTwo, chrJobThree);
			chrJobThree = chrJobThreeList.get(0).getCate3_name();
			selectMap.put("chrJobThreeList",chrJobThreeList);
		}else{
            selectMap.put("chrJobThree",chrJobThree);
        }	
		
		path = chrSelectPath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	/**
	 * 中华英才流量来源级联组件内容获取
	 */
	private String dealCHRFlowSourceSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");
		String path = "";
		
		selectMap.put("showCHRSourceSelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showCHRSource"+(i+1), 1);
			}
		}
		int chrFlowShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("chrFlowShowType", chrFlowShowType);
		String chrFlowSourcePlatform = ""; //平台类型
		for(String temp : vo.getDetailValue().split(";")){
			if(temp.split(":")[0].equals("platform")){
				chrFlowSourcePlatform = temp.split(":")[1];
			}
		}
		selectMap.put("chrFlowSourcePlatform",chrFlowSourcePlatform);
		//一级来源  
		String chrSourceOne = ServletRequestUtils.getStringParameter(request,"chrSourceOne" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRSource1")){
			List<ChinahrSource> chrSourceOneList = chartService.getCHRSource1(chrFlowSourcePlatform, chrSourceOne);
			chrSourceOne = chrSourceOneList.get(0).getSource1();
			selectMap.put("chrSourceOneList",chrSourceOneList);
		}else{
			selectMap.put("chrSourceOne",chrSourceOne);
		}
		
		//二级来源  
		String chrSourceTwo = ServletRequestUtils.getStringParameter(request,"chrSourceTwo" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRSource2")){
			List<ChinahrSource> chrSourceTwoList = chartService.getCHRSource2(chrFlowSourcePlatform, chrSourceOne, chrSourceTwo);
			chrSourceTwo = chrSourceTwoList.get(0).getSource2();
			selectMap.put("chrSourceTwoList",chrSourceTwoList);
		}else{
			selectMap.put("chrSourceTwo",chrSourceTwo);
		}
		
		//三级来源  
		String chrSourceThree = ServletRequestUtils.getStringParameter(request,"chrSourceThree" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRSource3")){
			List<ChinahrSource> chrSourceThreeList = chartService.getCHRSource3(chrFlowSourcePlatform, chrSourceOne, chrSourceTwo, chrSourceThree);
			chrSourceThree = chrSourceThreeList.get(0).getSource3();
			selectMap.put("chrSourceThreeList",chrSourceThreeList);
		}else{
			selectMap.put("chrSourceThree",chrSourceThree);
		}
		
		//四级来源  
		String chrSourceFour = ServletRequestUtils.getStringParameter(request,"chrSourceFour" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showCHRSource4")){
			List<ChinahrSource> chrSourceFourList = chartService.getCHRSource4(chrFlowSourcePlatform,chrSourceOne, chrSourceTwo, chrSourceThree, chrSourceFour);
			chrSourceFour = chrSourceFourList.get(0).getSource4();
			selectMap.put("chrSourceFourList",chrSourceFourList);
		}else{
			selectMap.put("chrSourceFour",chrSourceFour);
		}
		path = chrSelectPath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	/**
	 * 流量来源级联组件内容获取
	 */
	private String dealFlowSourceSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";
		
		selectMap.put("showFlowSourceSelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showFlowSource"+(i+1), 1);
			}
		}
		int flowShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("flowShowType", flowShowType);
		String flowSourcePlatform = ""; //平台类型
		for(String temp : vo.getDetailValue().split(";")){
			if(temp.split(":")[0].equals("platform")){
				flowSourcePlatform = temp.split(":")[1];
			}
		}
		selectMap.put("flowSourcePlatform" , flowSourcePlatform);
		//一级来源
		String flowSourceOne = ServletRequestUtils.getStringParameter(request,"flowSourceOne" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showFlowSource1")){
			//一级来源完整列表
			List<NetFlowSource> flowSourceOneList = chartService.getSource1List(flowSourcePlatform,flowSourceOne);
			//一级来源展示列表
			List<NetFlowSource> finalFlowSourceOneList = new ArrayList<NetFlowSource>();
			User user = spu.getUserByCookie(request);
			if(MyStore.admin.equals(user.getFunction())){ //管理员权限
				finalFlowSourceOneList = flowSourceOneList;
			}else{
				//流量来源一级类权限控制处理
				Map<String,NetFlowSource> flowSourceOneMap = new HashMap<String, NetFlowSource>();
				for (NetFlowSource netFlowSource : flowSourceOneList) {
					flowSourceOneMap.put(netFlowSource.getSource1(), netFlowSource);
				}
				
				HashSet<Long> userOpersSet = userOperService.getUserOpers(user.getId());//用户全部细粒度权限
				List<OperMenu> operList = userOperService.getOperListByCate("流量数据统计", "流量来源一级渠道"); //该系列所有正常数据权限
				
				String checkFowSourceOne = "";
				for (OperMenu operMenu : operList) {
					long operid = operMenu.getId();
					String operName = operMenu.getName();
					if(userOpersSet.contains(operid)){
						if(operid == 9){ //所有一级来源
							finalFlowSourceOneList = flowSourceOneList;
							break;
						}else{
							if(flowSourceOneMap.containsKey(operName)){
								finalFlowSourceOneList.add(flowSourceOneMap.get(operName));
								if("".equals(flowSourceOne)){ //全选时
									checkFowSourceOne = checkFowSourceOne + "'" + operName+"',";
								}
							}
							
						}
					}
				}
				
				if("".equals(flowSourceOne)){ //全选时
					flowSourceOne = checkFowSourceOne;
				}
				if(flowSourceOne.endsWith(",")){
					flowSourceOne = flowSourceOne.substring(0,flowSourceOne.lastIndexOf(","));
				}
			}
			selectMap.put("flowSourceOneList",finalFlowSourceOneList);
		}else{
			selectMap.put("flowSourceOne",flowSourceOne);
		}
		//二级来源
		String flowSourceTwo = ServletRequestUtils.getStringParameter(request,"flowSourceTwo" ,"");
		if(selectMap.containsKey("showFlowSource2")){
			List<NetFlowSource> flowSourceTwoList = chartService.getSource2List(flowSourcePlatform,flowSourceOne, flowSourceTwo);
			selectMap.put("flowSourceTwoList",flowSourceTwoList);
		}else{
			selectMap.put("flowSourceTwo",flowSourceTwo);
		}
		//三级来源
		String flowSourceThree = ServletRequestUtils.getStringParameter(request,"flowSourceThree" ,"");
		if(selectMap.containsKey("showFlowSource3")){
			List<NetFlowSource> flowSourceThreeList = chartService.getSource3List(flowSourcePlatform,flowSourceOne, flowSourceTwo, flowSourceThree);
			selectMap.put("flowSourceThreeList",flowSourceThreeList);
		}else{
			selectMap.put("flowSourceTwo",flowSourceTwo);
		}
		
		//四级来源
		String flowSourceFour = ServletRequestUtils.getStringParameter(request,"flowSourceFour" ,"");
		if(selectMap.containsKey("showFlowSource4")){
			List<NetFlowSource> flowSourceFourList = chartService.getSource4List(flowSourceOne, flowSourceTwo, flowSourceThree, flowSourceFour);
			selectMap.put("flowSourceFourList",flowSourceFourList);
		}else{
			selectMap.put("flowSourceFour",flowSourceFour);
		}
		path = infoSourcePath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	/**
	 * 信息来源级联组件内容获取
	 */
	private String dealInfoSourceSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";
		
		selectMap.put("showInfoSelects", 1);
		for(int i = 0; i< senames.length ; i++ ){
			String item = senames[i];
			if(StringUtils.contains(groupBy, item)){
				selectMap.put("showInfoSource"+i, 1);
			}
		}
		int infoShowType = 3; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
		selectMap.put("infoShowType", infoShowType);
		
		//平台
		String infoPlatform = ServletRequestUtils.getStringParameter(request,"infoPlatform" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showInfoSource0")){
			List<InfoSourcePlatform> infoPlatformList = chartService.getInfoPlatformList(infoPlatform);
			infoPlatform = infoPlatformList.get(0).getPlatform();
			selectMap.put("infoPlatformList",infoPlatformList);
		}else{
			selectMap.put("infoPlatform", infoPlatform);
		}
		//sourceName
		String infoSourceName = ServletRequestUtils.getStringParameter(request,"infoSourceName" ,"").replaceAll("'| ", "");
		if(selectMap.containsKey("showInfoSource1")){
				List<InfoSourcePlatform> infoSourceList = chartService.getInfoSourceList(infoPlatform, infoSourceName);
				selectMap.put("infoSourceList",infoSourceList);
		}else{
			selectMap.put("infoSourceName", infoSourceName);
		}
		path = infoSourcePath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
		
	}

	/**级联组件内容获取
	 */
	private String dealCitySelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");		
		String path = "";
		User user = spu.getUserByCookie(request);

		if(vo.getSearchTypeId()  == 11){//展现城市级联
			selectMap.put("showDL", 1);
			for(int i = 0; i< senames.length ; i++ ){
				String item = senames[i];
				if(StringUtils.contains(groupBy, item)){
					selectMap.put("showDL"+(i+1), 1);
				}
			}
			
			int cityShowType = 0; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
			for(String temp : vo.getDetailValue().split(";")){
				if(temp.split(":")[0].equals("type")){
					cityShowType = Integer.parseInt(temp.split(":")[1]);
				}
			}
			selectMap.put("cityShowType", cityShowType);
			
			//一级城市
			String dlCity1 = ServletRequestUtils.getStringParameter(request,"dlCityOne" ,"").replaceAll("'| ", "");
			
			List<DisplayLocal> dlCityOneList =  new ArrayList<DisplayLocal>();
			//权限检查
			String operCitys = IntReportMainService.cityOperCheck(user);
			//全部城市权限才展示 直营&中小城市 这2个特殊选择
			if(MyStore.cityOperAll.equals(operCitys)){ 
				selectMap.put("specity1",1);
			}
			if(selectMap.containsKey("showDL1")){
				//城市未选&&权限非全部城市   则 需要根据权限进行限定条件
				if("".equals(dlCity1) ){
					if(!MyStore.cityOperAll.equals(operCitys)){
						if(MyStore.cityOperZY.equals(operCitys)){ //直营
							dlCity1 = MyStore.zycitys;
						}else if(MyStore.cityOperQD.equals(operCitys)){//渠道
							dlCity1 = MyStore.cityOperQD; //渠道比较特殊  先赋予一个值 后面查询条件用not in作特殊处理
						}else { //其他
							dlCity1 = operCitys.substring(0,operCitys.lastIndexOf(","));
						}
						dlCity1 = dlCity1.replaceAll("'| ", "");
						dlCityOneList = chartService.getDisplayLevelOne(dlCity1);
					}else{
						dlCityOneList = chartService.getDisplayCitysByOpers(operCitys);
					}
				} else{
					dlCityOneList = chartService.getDisplayLevelOne(dlCity1);
				}
				dlCity1 = dlCityOneList.get(0).getCity1();
				selectMap.put("dlCityOneList",dlCityOneList);
			}
			

			//全部渠道城市选择特殊处理  
			if(MyStore.cityOperQD.equals(dlCity1)){
				selectMap.put("dlCityOne","");
			}else{
				selectMap.put("dlCityOne",dlCity1);
			}
			
			//二级城市
			String dlCity2 = ServletRequestUtils.getStringParameter(request,"dlCityTwo" ,"").replaceAll("'| ", "");
			if(selectMap.containsKey("showDL2")){
					List<DisplayLocal> dlCityTwoList = chartService.getDisplayLevelTwo(dlCity1,dlCity2);
					dlCity2 = dlCityTwoList.get(0).getCity2();
					selectMap.put("dlCityTwoList",dlCityTwoList);
			}
			
			String dlCity3 = ServletRequestUtils.getStringParameter(request,"dlCityThree","");
			if(selectMap.containsKey("showDL3")){
				List<DisplayLocal> dlCityThreeList = chartService.getDisplayLevelThree(dlCity1, dlCity2, dlCity3);
				dlCity3 = dlCityThreeList.get(0).getCity3();
				selectMap.put("dlCityThreeList",dlCityThreeList);
			}
		}else if(vo.getSearchTypeId()  == 12){//归属城市级联
			
			for(int i = 0; i< senames.length ; i++ ){
				String item = senames[i];
				if(StringUtils.contains(groupBy, item)){
					selectMap.put("showBL", 1);
					selectMap.put("showBL"+(i+1), 1);
				}
			}
			int cityShowType = 0; //查询类型  1：正常   2：兼容老的向上汇总报表(后期去除)   3.新的向上汇总
			for(String temp : vo.getDetailValue().split(";")){
				if(temp.split(":")[0].equals("type")){
					cityShowType = Integer.parseInt(temp.split(":")[1]);
				}
			}
			selectMap.put("cityShowType", cityShowType);
			
			//一级城市
			String blCity1 = ServletRequestUtils.getStringParameter(request,"blCityOne" ,"").replaceAll("'| ", "");
			
			List<BelongLocal> blCityOneList = new ArrayList<BelongLocal>();
			//权限检查
			String operCitysBelong = IntReportMainService.cityOperCheck(user);
			//全部城市权限才展示 直营&中小城市 这2个特殊选择
			if(MyStore.cityOperAll.equals(operCitysBelong)){ 
				selectMap.put("specity1",1);
			}
			
			if(selectMap.containsKey("showBL1")){
				//城市未选&&权限非全部城市   则 需要根据权限进行限定条件				
				if("".equals(blCity1) ){
					if(!MyStore.cityOperAll.equals(operCitysBelong)){
						if(MyStore.cityOperZY.equals(operCitysBelong)){ //直营
							blCity1 = MyStore.zycitys;
						}else if(MyStore.cityOperQD.equals(operCitysBelong)){//渠道
							blCity1 = MyStore.cityOperQD; //渠道比较特殊  先赋予一个值 后面查询条件用not in作特殊处理
						}else { //其他
							blCity1 = operCitysBelong.substring(0,operCitysBelong.lastIndexOf(","));
						}
						blCity1 = blCity1.replaceAll("'| ", "");
						blCityOneList = chartService.getBelongLevelOne(blCity1);
					}else{
						blCityOneList = chartService.getBelongCitysByOpers(operCitysBelong);
					}
				} else{
					blCityOneList = chartService.getBelongLevelOne(blCity1);
				}
				blCity1=blCityOneList.get(0).getCity1();
				selectMap.put("blCityOneList",blCityOneList);
//				if(MyStore.cityOperQD.equals(blCity1)){
//					blCity1 = ""; //渠道城市 city1条件使用完了 要改回正常状态 否则影响后续使用
//				}
			}
			//二级城市
			String blCity2 = ServletRequestUtils.getStringParameter(request,"blCityTwo" ,"").replaceAll("'| ", "");
			if(selectMap.containsKey("showBL2")){
					List<BelongLocal> blCityTwoList = chartService.getBelongLevelTwo(blCity1,blCity2);
					blCity2=blCityTwoList.get(0).getCity2();
					selectMap.put("blCityTwoList",blCityTwoList);										
			}
			
			String blCity3 = ServletRequestUtils.getStringParameter(request,"blCityThree" ,"").replaceAll("'| ", "");
			if(selectMap.containsKey("showBL3")){
				List<BelongLocal> blCityThreeList = chartService.getBelongLevelThree(blCity1, blCity2, blCity3);
				blCity3=blCityThreeList.get(0).getCity3();
				selectMap.put("blCityThreeList",blCityThreeList);
			}			
		}
		
		path = citySelectPath;
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
		
	/** 类别 级联组件内容获取
	 * @param vo
	 * @return
	 */
	private String dealCateSelect(HttpServletRequest request, IRSearchConfig vo,String groupBy)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String senames[] = vo.getSename().split(";");
		
		String detailValue = vo.getDetailValue();
		String showBusi = ""; //新业务线类别组件   业务线展示限定参数
		//参数获取
	//	String[] tempArrays = detailValue.split(";");
	//	User user = spu.getUserByCookie(request);
		String path = "";
		if(vo.getSearchTypeId()  == 17){
			boolean isBiz = false; //业务线类型  默认为用户产品业务线
			for(int i = 0; i< senames.length ; i++ ){
				String item = senames[i];
				if(StringUtils.contains(groupBy, item)){
					selectMap.put("showNewDP", 1);
					selectMap.put("showNewDP"+i, 1);
				}
			}
			int cateShowType = 0; //查询类型  1：正常   3：向上汇总
		
			for(String temp : detailValue.split(";")){
				if(temp.contains("show")){ //报表配置业务线展现限定  - like : show:huangye,ershou
					showBusi = temp.split(":")[1];
				}
				if(temp.split(":")[0].equals("type")){
					cateShowType = Integer.parseInt(temp.split(":")[1]);
				}
				if(temp.split(":")[0].equals("isBiz") && "1".equals(temp.split(":")[1].trim())){ //业务线类型   - like : isBiz:1      1=商业业务线  0 = 用户业务线
					isBiz = true;
				}
			}			
			selectMap.put("isBiz", isBiz);
			selectMap.put("cateShowType", cateShowType);
			//用户操作权限 - 业务线判定
		//	String dispCheckResult = IntReportMainService.businessOperCheck(user, showBusi);
			
			//频道
			String dpBusinessTypeName = ServletRequestUtils.getStringParameter(request,"dpBusinessTypeName" ,"");
			dpBusinessTypeName = dpBusinessTypeName.replaceAll("'| ", "");
			if(selectMap.containsKey("showNewDP0")){
				if("".equals(dpBusinessTypeName) && !"".equals(showBusi)){
					for (String temp : showBusi.split(",")) {
						dpBusinessTypeName = dpBusinessTypeName + "'" + temp+"',";
					}
					dpBusinessTypeName = dpBusinessTypeName.substring(0,dpBusinessTypeName.lastIndexOf(","));
				}
				List<NewDisplayCate> dpBusinessTypeList = chartService.getAllBusinessType(dpBusinessTypeName , isBiz);//业务线列表
				if(isBiz){
					dpBusinessTypeName = dpBusinessTypeList.get(0).getBusiness_type_biz();
				}else{
					dpBusinessTypeName = dpBusinessTypeList.get(0).getBusiness_type();
				}
				
				selectMap.put("dpBusinessTypeList",dpBusinessTypeList);
			}else{
				selectMap.put("dpBusinessTypeName",dpBusinessTypeName);
			}
			
			//一级品类
			String dpNewCateOne = ServletRequestUtils.getStringParameter(request,"dpNewCateOne" ,"").replaceAll("'| ", "");
			List<NewDisplayCate> dpNewCateOneList = new ArrayList<NewDisplayCate>();
			
			if(selectMap.containsKey("showNewDP1")){
				dpNewCateOneList = chartService.getLevelOne(dpBusinessTypeName, dpNewCateOne, isBiz);//根据业务线获取对应一级类
				dpNewCateOne = dpNewCateOneList.get(0).getCate1_name();
	//			selectMap.put("dpNewCateOne",dpNewCateOne);
				selectMap.put("dpNewCateOneList",dpNewCateOneList);
			}else{
				selectMap.put("dpNewCateOne",dpNewCateOne);
			}
			
			
			//二级品类
			String dpNewCateTwo = ServletRequestUtils.getStringParameter(request,"dpNewCateTwo" ,"").replaceAll("'| ", "");
			if(selectMap.containsKey("showNewDP2")){
				List<NewDisplayCate> dpNewCateTwoList = new ArrayList<NewDisplayCate>();
				dpNewCateTwoList = chartService.getLevelTwo(dpBusinessTypeName,dpNewCateOne, dpNewCateTwo, isBiz);
				dpNewCateTwo = dpNewCateTwoList.get(0).getCate2_name();
				selectMap.put("dpNewCateTwo",dpNewCateTwo);
				selectMap.put("dpNewCateTwoList",dpNewCateTwoList);
			}else{
				selectMap.put("dpNewCateTwo",dpNewCateTwo);
			}
			//三级品类
			String dpNewCateThree = ServletRequestUtils.getStringParameter(request,"dpNewCateThree","").replaceAll("'| ", "");
			if(selectMap.containsKey("showNewDP3")){
				List<NewDisplayCate> dpNewCateThreeList = chartService.getLevelThree(dpBusinessTypeName,dpNewCateOne,dpNewCateTwo, dpNewCateThree , isBiz);
				dpNewCateThree = dpNewCateThreeList.get(0).getCate3_name();
				selectMap.put("dpNewCateThree",dpNewCateThree);
				selectMap.put("dpNewCateThreeList",dpNewCateThreeList);
			}else{
				selectMap.put("dpNewCateThree",dpNewCateThree);
			}
				
		}else if(vo.getSearchTypeId()  == 18){//归属品类级联 new
				selectMap.put("showNewBC", 1);
				for(int i = 0; i< senames.length ; i++ ){
					String item = senames[i];
					if(StringUtils.contains(groupBy, item)){
						selectMap.put("showNewBC"+i, 1);
					}
				}
				
				int cateShowType = 0; //查询类型  1：正常   3：向上汇总
				String newBcshowBuis = "";//展示业务线限定参数  无限定则默认为空 = 全部展示
				boolean isBiz = false;
				for(String temp : vo.getDetailValue().split(";")){
					if(temp.split(":")[0].equals("show")){
						newBcshowBuis = temp.split(":")[1];
					}else if(temp.split(":")[0].equals("isBiz") && "1".equals(temp.split(":")[1].trim())){ //业务线类型   - like : isBiz:1      1=商业业务线  0 = 用户业务线
						isBiz = true;
					}
					if(temp.split(":")[0].equals("type")){
						cateShowType = Integer.parseInt(temp.split(":")[1]);
					}
				}
				selectMap.put("isBiz",isBiz);
				selectMap.put("cateShowType",cateShowType);
				
	//			用户操作权限 - 业务线判定
	//			String belongCheckResult = IntReportMainService.businessOperCheck(user, newBcshowBuis);
				//频道
				String bcBusinessTypeName = ServletRequestUtils.getStringParameter(request,"bcBusinessTypeName" ,"").replaceAll("'| ", "");
				if(selectMap.containsKey("showNewDP0")){
					if("".equals(bcBusinessTypeName) && !"".equals(newBcshowBuis)){
						for (String temp : newBcshowBuis.split(",")) {
							bcBusinessTypeName = bcBusinessTypeName + "'" + temp+"',";
						}
						bcBusinessTypeName = bcBusinessTypeName.substring(0,bcBusinessTypeName.lastIndexOf(","));
					}
					
//					selectMap.put("bcBusinessTypeName",bcBusinessTypeName);
					List<NewBelongCate> bcBusinessTypeList = chartService.getAllBLBusinessType(bcBusinessTypeName,isBiz);//业务线列表
					if(isBiz){
						bcBusinessTypeName = bcBusinessTypeList.get(0).getBusiness_type_biz();
					}else{
						bcBusinessTypeName = bcBusinessTypeList.get(0).getBusiness_type();
					}

					selectMap.put("bcBusinessTypeList",bcBusinessTypeList);
				}else{
					selectMap.put("bcBusinessTypeName",bcBusinessTypeName);
				}
				//一级品类
				String bcNewCateOne = ServletRequestUtils.getStringParameter(request,"bcNewCateOne" ,"").replaceAll("'| ", "");
				if(selectMap.containsKey("showNewBC1")){ // 表内无业务线字段的  把一级类全部拿出来
					List<NewBelongCate> bcNewCateOneList = new ArrayList<NewBelongCate>();
					bcNewCateOneList = chartService.getBLLevelOne(bcBusinessTypeName, bcNewCateOne, isBiz);
					bcNewCateOne = bcNewCateOneList.get(0).getCate1_name();
					selectMap.put("bcNewCateOneList",bcNewCateOneList);
//					selectMap.put("bcNewCateOne",bcNewCateOne);
				}else{
					selectMap.put("bcNewCateOne",bcNewCateOne);
				}
				
				//二级品类
				String bcNewCateTwo = ServletRequestUtils.getStringParameter(request,"bcNewCateTwo" ,"").replaceAll("'| ", "");
				if(selectMap.containsKey("showNewBC2")){
					List<NewBelongCate> bcNewCateTwoList = new ArrayList<NewBelongCate>();
					bcNewCateTwoList = chartService.getBLLevelTwo(bcBusinessTypeName, bcNewCateOne, bcNewCateTwo, isBiz);
					bcNewCateTwo = bcNewCateTwoList.get(0).getCate2_name();
					selectMap.put("bcNewCateTwoList",bcNewCateTwoList);
//					selectMap.put("bcNewCateTwo",bcNewCateTwo);
				}else{
					selectMap.put("bcNewCateTwo",bcNewCateTwo);
				}
				//三级品类
				String bcNewCateThree = ServletRequestUtils.getStringParameter(request,"bcNewCateThree","").replaceAll("'| ", "");
				if(selectMap.containsKey("showNewBC3")){
						List<NewBelongCate> bcNewCateThreeList = chartService.getBLLevelThree(bcBusinessTypeName, bcNewCateOne,  bcNewCateTwo, bcNewCateThree, isBiz);
						bcNewCateThree = bcNewCateThreeList.get(0).getCate3_name();
						selectMap.put("bcNewCateThreeList",bcNewCateThreeList);
//						selectMap.put("bcNewCateThree",bcNewCateThree);
				}else{
					selectMap.put("bcNewCateThree",bcNewCateThree);
				}
			}		
			path = cateSelectPath;
			result = MyPathUtil.getTemByPath(path);
			result  = VelocityTools.render( result, selectMap);
			return result;
		}
}


